package testcase.app;

import java.sql.Connection;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import api.app.ApiUserAdd;
import api.app.GetVerificationCode;
import api.app.VerificationPhoneCode;
import cn.bmyi.sms.Sms;
import common.Config;
import common.DB;
import common.Host;
import common.TestData;

/**
 * 帮忙医APP用户注册TestApiUserAdd接口测试用例
 * 
 * @author WuZF
 *
 */
public class TestApiUserAdd
{

	/**
	 * 帮忙医APP用户注册TestApiUserAdd接口测试环境数据驱动
	 * 
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider(name = "Test")
	public static String[][] testApiUserAddDataProvider() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestApiUserAdd.xml");
		return td.getParam();
	}

	/**
	 * 帮忙医APP用户注册TestApiUserAdd接口
	 * 
	 * @param caseName
	 *            -用例名称
	 * @param userAccount
	 *            -电话号码
	 * @param Password
	 *            -密码
	 * @throws Exception
	 */
	@Test(dataProvider = "Test", groups = { "Test" })
	public void testApiUserAddTestCase(String caseName, String userAccount, String password) throws Exception
	{
		Host.setup("test");

		// 获取并校验短信验证码
		if (caseName.equals("RegisteredSuccess"))
		{
			String getVerificationCodeResult = GetVerificationCode.post(userAccount);
			boolean isMobileExist = new JSONObject(getVerificationCodeResult).getJSONObject("Data")
					.getBoolean("MobileExist");
			if (isMobileExist)
			{
				System.out.println(getVerificationCodeResult);
				Assert.fail("获取验证码失败");// 判断验证码是否获取成功，如果没有获取成功，测试用例执行失败
			}
			String sms = Sms.getVerificationCodeRepeatedly(userAccount);
			String verificationPhoneCodeResult = VerificationPhoneCode.post(userAccount, sms);
			verificationPhoneCodeResult = new JSONObject(verificationPhoneCodeResult).getJSONObject("Data")
					.getString("verificationstatus");
			if (!verificationPhoneCodeResult.equals("Success"))
			{
				Assert.fail("校验验证码失败");// 判断验证码校验是否成功，如果没有获取成功，测试用例执行失败
			}

		}

		// 调用注册接口
		String apiResult = ApiUserAdd.post(userAccount, password);
		JSONObject jsonResult;
		if (apiResult == null || apiResult.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(apiResult);
		}
		String checkpoint;
		switch (caseName)
		{
			case "RegisteredFail":
				checkpoint = jsonResult.getString("Message");
				Assert.assertEquals(checkpoint, "注册失败");
				break;
			case "RegisteredSuccess":
				checkpoint = jsonResult.getString("Message");
				Assert.assertEquals(checkpoint, "注册成功");
				break;
		}
	}

	// 注册成功后，数据库清除数据
	@AfterTest(groups = { "Test" })
	public void afterTest() throws Exception
	{

		Host.setup("Test");
		String bmydriver = Config.getValue("BmyiSQLServer-Test", "BmyiDriver");
		String bmyurl = Config.getValue("BmyiSQLServer-Test", "BmyiUrl");
		String bmyuser = Config.getValue("BmyiSQLServer-Test", "BmyiUser");
		String bmypwd = Config.getValue("BmyiSQLServer-Test", "BmyiPassword");
		Connection connection = DB.connect(bmydriver, bmyurl, bmyuser, bmypwd);// 连接帮忙医SQLServer

		TestData tData = new TestData("app/TestApiUserAdd.xml");
		String mobileData = tData.getData("RegisteredSuccess", "userAccount");// 读取TestApiUserAdd.XML文件内容
		String sql = "DELETE FROM SYS_Users WHERE Mobilephone='" + mobileData + "'";
		DB.update(connection, sql);// 实现SYS_User表数据内容删除

		String crmdriver = Config.getValue("CrmSQLServer-Test", "CrmDriver");
		String crmurl = Config.getValue("CrmSQLServer-Test", "CrmUrl");
		String crmuser = Config.getValue("CrmSQLServer-Test", "CrmUser");
		String crmpwd = Config.getValue("CrmSQLServer-Test", "CrmPassword");
		connection = DB.connect(crmdriver, crmurl, crmuser, crmpwd);// 连接CRMSQLServer
		sql = "DELETE FROM Member WHERE Mobile='" + mobileData + "'";
		DB.update(connection, sql);// 实现Member和WebAccount表数据内容删除

		connection = DB.connect(crmdriver, crmurl, crmuser, crmpwd);// 连接CRMSQLServer
		sql = "DELETE FROM WebAccount WHERE Mobile='" + mobileData + "'";
		DB.update(connection, sql);// 实现Member和WebAccount表数据内容删除

	}
}
